﻿#include "draw_pix.h"
#include "ui_draw_pix.h"

#include <qmath.h>

draw_pix::draw_pix(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::draw_pix)
{
    ui->setupUi(this);

    setMouseTracking(true);//开启鼠标实时追踪，监听鼠标移动事件，默认只有按下时才监听


}

draw_pix::~draw_pix()
{
    delete ui;
}

void draw_pix::paintEvent(QPaintEvent *e)
{
//    QWidget::paintEvent(e);
//    QPainter painter(this);
//    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
//    painter.drawPixmap(0, 0, m_pixmap);
    QPainter painter1(this);
    QPixmap pixmap(this->width(),this->height());
    pixmap.fill(Qt::white);
    QPainter painter(&m_pixmap);
    painter.save();
    painter.translate(this->width()/2,this->height()/2);  //提前旋转一个坐标系
    for(int j = 0;j<d_all_store->p_text.size();j++)
    {
        QString content = d_all_store->p_text[j].text;
        qreal q = d_all_store->p_text[j].angle;  //起始旋转角度
        QFont font = painter.font();
        //font.setFamily("Microsoft YaHei");
        font.setFamily(d_all_store->p_text[j].text_family);
        font.setPointSize(d_all_store->p_text[j].font_size);   //设置字体大小
        font.setWeight(60);
        painter.setFont(font);
        //painter.setPen(QPen(Qt::blue, 7));
        qreal word_spaing = (d_all_store->p_text[j].word_spacing-q)/content.length(); //计算字间距
        for(int i = 0; i < content.length(); i++)
        {
            painter.save();
            double radian = q*3.14/180;
            painter.translate(d_all_store->p_text[j].radius*cos(radian),d_all_store->p_text[j].radius*sin(radian));
            qDebug()<<d_all_store->p_text[j].radius<<"字体半径";
            painter.rotate(q + 95);
            const QChar && charTxt = content.at(i);
            painter.drawText(QPoint(0,0),charTxt);
            //q = q+20;
            //q = q + d_all_store->p_text[j].word_spacing; //调整字间距
            q = q + word_spaing; //调整字间距
            painter.restore();
        }
    }
//    for(int ii = 0;ii<d_all_store->sel_picture.size();ii++)
//    {
//        int i = d_all_store->sel_picture[ii];   //从被选中的图案编号选取数字
//        for(int j = 0;j<d_all_store->p_picture[i].p_rect.size();j++)
//        {
//            qreal q1 = d_all_store->p_picture[i].angle;
//            qreal x = d_all_store->p_picture[i].p_rect[j].x; //图案内角度计算
//            qreal y = d_all_store->p_picture[i].p_rect[j].y;
//            qreal width = d_all_store->p_picture[i].p_rect[j].width;
//            qreal height = d_all_store->p_picture[i].p_rect[j].height;
//            int radius = d_all_store->p_picture[i].radius;
//            qDebug()<<q1<<x<<y<<width<<height<<radius;
//            painter.save();
//            double radian = q1*3.14/180;   //第一次旋转
//            painter.translate(radius * cos(radian),radius * sin(radian));
//            painter.rotate(q1+90);
//            //painter.translate((x+width)/2,(height + y)/2);  //新加的
//            painter.translate(x,y);  //新加的
//            //painter.drawRect(-d_all_store->p_picture[i].p_rect[j].width/2,-d_all_store->p_picture[i].p_rect[j].height/2,d_all_store->p_picture[i].p_rect[j].width,d_all_store->p_picture[i].p_rect[j].height);
//            painter.drawRect(0,0,width,height);
//            painter.restore();
//        }
//        for(int j = 0;j<d_all_store->p_picture[i].p_ellipse.size();j++)
//        {
//            qreal q1 = d_all_store->p_picture[i].angle;
//            qreal x = d_all_store->p_picture[i].p_ellipse[j].x; //图案内角度计算
//            qreal y = d_all_store->p_picture[i].p_ellipse[j].y;
//            qreal width = d_all_store->p_picture[i].p_ellipse[j].width;
//            qreal length = d_all_store->p_picture[i].p_ellipse[j].length;
//            int radius = d_all_store->p_picture[i].radius;  //计算总的半径
//            painter.save();
//            double radian = (q1)*3.14/180;
//            painter.translate(radius * cos(radian), radius * sin(radian));
//            painter.rotate(q1+90);
//            painter.translate(x,y);  //新加的
//            painter.drawEllipse(QPoint(0,0), length, width);
//            //painter.drawEllipse(, d_all_store->p_picture[i].p_ellipse[j].length, d_all_store->p_picture[i].p_ellipse[j].width);
//            painter.restore();
//        }
//    }
    for(int ii = 0;ii<d_all_store->p_layer.size();ii++)
    {
        qreal scale = d_all_store->p_layer[ii].scale;   //比例
        int i = d_all_store->p_layer[ii].sel_picture;
        int radius = d_all_store->p_layer[ii].radius+(460*scale/5);  //加的是画布高度，5是一开始的比例
        //int radius = d_all_store->p_layer[ii].radius;
        int angle = d_all_store->p_layer[ii].angle;


        double radian = angle*3.14/180;   //第一次旋转
        painter.save();
        painter.translate(radius * cos(radian),radius * sin(radian));    //先将坐标平移到画布位置
        painter.rotate(angle+90);
        painter.translate(-781*scale/(2*5),0);  //以画布位置的中心进行校准
        for(int j = 0;j<d_all_store->p_picture[i].p_rect.size();j++)
        {
            qreal x = d_all_store->p_picture[i].p_rect[j].x * scale; //图案内角度计算
            qreal y = d_all_store->p_picture[i].p_rect[j].y * scale;
            qreal width = d_all_store->p_picture[i].p_rect[j].width * scale;
            qreal height = d_all_store->p_picture[i].p_rect[j].height * scale;
            //int radius = d_all_store->p_picture[i].radius;
            qDebug()<<angle<<x<<y<<width<<height<<radius;
            painter.save();
//            double radian = angle*3.14/180;   //第一次旋转
//            painter.translate(radius * cos(radian),radius * sin(radian));    //先将坐标平移到画布位置
//            painter.rotate(angle+90);
//            painter.translate(-781/(2*5),0);  //以画布位置的中心进行校准
            //painter.translate((x+width)/2,(height + y)/2);  //新加的
            painter.translate(x,y);  //通过这个来校准矩形位置
            //painter.drawRect(-d_all_store->p_picture[i].p_rect[j].width/2,-d_all_store->p_picture[i].p_rect[j].height/2,d_all_store->p_picture[i].p_rect[j].width,d_all_store->p_picture[i].p_rect[j].height);
            painter.drawRect(0,0,width,height);
            painter.restore();
        }
        for(int j = 0;j<d_all_store->p_picture[i].p_ellipse.size();j++)
        {
            //qreal q1 = d_all_store->p_picture[i].angle;
            qreal x = d_all_store->p_picture[i].p_ellipse[j].x * scale; //图案内角度计算
            qreal y = d_all_store->p_picture[i].p_ellipse[j].y * scale;
            qreal width = d_all_store->p_picture[i].p_ellipse[j].width * scale;
            qreal length = d_all_store->p_picture[i].p_ellipse[j].length * scale;
            //int radius = d_all_store->p_picture[i].radius;  //计算总的半径
            painter.save();
//            double radian = (angle)*3.14/180;
//            painter.translate(radius * cos(radian), radius * sin(radian));
//            painter.rotate(angle+90);
            painter.translate(x,y);  //新加的
            painter.drawEllipse(QPointF(0,0), length, width);
            //painter.drawEllipse(, d_all_store->p_picture[i].p_ellipse[j].length, d_all_store->p_picture[i].p_ellipse[j].width);
            painter.restore();
        }
        for(int j = 0;j<d_all_store->p_picture[i].p_line.size();j++)
        {
            QPoint point1 = d_all_store->p_picture[i].p_line[j].startPoint * scale;
            QPoint point2 = d_all_store->p_picture[i].p_line[j].endPoint * scale;
            //int radius = d_all_store->p_picture[i].radius;  //计算总的半径
            painter.save();
//            double radian = (angle)*3.14/180;
//            painter.translate(radius * cos(radian), radius * sin(radian));
//            painter.rotate(angle+90);
            //painter.translate(x,y);  //新加的
            painter.drawLine(point1,point2);
            //painter.drawEllipse(, d_all_store->p_picture[i].p_ellipse[j].length, d_all_store->p_picture[i].p_ellipse[j].width);
            painter.restore();
        }
        for(int j = 0;j<d_all_store->p_picture[i].p_curve.size();j++)
        {
            QPoint startpoint = d_all_store->p_picture[i].p_curve[j].startPoint * scale;
            QPoint point1 = d_all_store->p_picture[i].p_curve[j].point1 * scale;
            QPoint point2 = d_all_store->p_picture[i].p_curve[j].point2 * scale;
            QPoint endpoint = d_all_store->p_picture[i].p_curve[j].endPoint * scale;
            //int radius = d_all_store->p_picture[i].radius;  //计算总的半径
            painter.save();
//            double radian = (angle)*3.14/180;
//            painter.translate(radius * cos(radian), radius * sin(radian));
//            painter.rotate(angle+90);

            QPainterPath myPath;
            myPath.moveTo(startpoint);
            myPath.cubicTo(point1, point2, endpoint);
            painter.drawPath(myPath);

            painter.restore();
        }
        painter.restore();
    }
    //给文字涂色区域
    if(flag == 1)  //如果选中文字
    {
        painter.save();
        painter.setBrush(QColor(0,170,255,100));
        painter.drawPath(d_all_store->p_text[text_point].path);
        flag = 0;
        painter.restore();
    }
    painter.drawEllipse(QPoint(0,0),300,300);
    painter.drawLine(QPoint(0,0),QPoint(350,0));
    painter.restore();
    painter1.drawPixmap(0, 0, m_pixmap);

}

void draw_pix::updacanvasPixmap(patternstore *all_store)
{
    d_all_store = all_store;
    QPixmap pixmap(this->width(),this->height());
    pixmap.fill(Qt::white);
    QPainter painter(&pixmap);
    painter.translate(this->width()/2,this->height()/2);  //提前旋转一个坐标系
//    //写文字
//    for(int j = 0;j<d_all_store->p_text.size();j++)
//    {
//        QString content = d_all_store->p_text[j].text;
//        qreal q = d_all_store->p_text[j].angle;  //旋转角度
//        qreal l = 0;
//        QFont font = painter.font();
//        font.setFamily("Microsoft YaHei");
//        font.setPointSize(d_all_store->p_text[j].font_size);   //设置字体大小
//        font.setWeight(99);   // 调整字体粗细
//        painter.setFont(font);

//        for(int i = 0; i < content.length(); i++)
//        {
//            painter.save();
//            double radian = q*M_PI/180;
//            //qDebug()<<radian;
//            painter.translate(d_all_store->p_text[j].radius*cos(radian),d_all_store->p_text[j].radius*sin(radian));
//           // painter.translate(0,10);
//            //painter.rotate(q + 95);
//            painter.rotate(q+90);
//            const QChar && charTxt = content.at(i);
//            l = painter.fontMetrics().width(charTxt) / 2.0;
//            //qDebug()<<painter.fontMetrics().width(charTxt);
//            painter.drawText(QPoint(-l,0),charTxt);
//            q = q+10;
//            painter.restore();
//        }
//    }
    //画图案
//    for(int i = 0;i<all_store.picture_count;i++)
//    {
//        for(int j = 0;j<all_store.p_picture[i].p_rect.size();j++)
//        {
//            qreal q = all_store.p_picture[i].p_rect[j].angle;
//            painter.save();
//            double radian = q*3.14/180;
//            painter.translate(all_store.p_picture[i].p_rect[j].radius*cos(radian),all_store.p_picture[i].p_rect[j].radius*sin(radian));
//            painter.rotate(q);
//            painter.drawRect(-all_store.p_picture[i].p_rect[j].width/2,-all_store.p_picture[i].p_rect[j].length/2,all_store.p_picture[i].p_rect[j].width,all_store.p_picture[i].p_rect[j].length);
//            painter.restore();
//        }
//        for(int j = 0;j<all_store.p_picture[i].p_ellipse.size();j++)
//        {
//            qreal q = all_store.p_picture[i].p_ellipse[j].angle;
//            painter.save();
//            double radian = q*3.14/180;
//            painter.translate(all_store.p_picture[i].p_ellipse[j].radius*cos(radian),all_store.p_picture[i].p_ellipse[j].radius*sin(radian));
//            painter.rotate(q);
//            painter.drawEllipse(QPoint(0,0), all_store.p_picture[i].p_ellipse[j].length, all_store.p_picture[i].p_ellipse[j].width);
//            painter.restore();
//        }
//    }

    //给文字涂色区域
//    if(flag == 1)  //如果选中文字
//    {
//        painter.save();
//        //qDebug()<<"???????";
//        painter.setBrush(QColor(0,170,255,100));
//        painter.drawPath(d_all_store->p_text[text_point].path);
//        flag = 0;
//        painter.restore();
//    }

    painter.drawEllipse(QPoint(0,0),300,300);
    m_pixmap = pixmap;
    update();
}

void draw_pix::mousePressEvent(QMouseEvent *event)
{
    QPainter painter(&m_pixmap);
    painter.save();
    painter.translate(this->width()/2,this->height()/2);  //提前旋转一个坐标系
    painter.setBrush(QColor(0,170,255,100));
    if(event->button() == Qt::LeftButton)
    {
       qDebug()<<event->pos();
       for(int i=0;i<d_all_store->p_text.size();i++)
       {
           qDebug()<<d_all_store->p_text[i].text;
           d_all_store->p_text[i].path.translate(this->width()/2,this->height()/2);
            if(d_all_store->p_text[i].path.contains(event->pos()))
            {
                d_all_store->p_text[i].path.translate(-this->width()/2,-this->height()/2);
                //painter.drawPath(f_all_store.p_text[i].path);
                //qDebug()<<"???????";
                flag = 1;
                text_point = i;  //记录需要改正的文字数组下表
                d_all_store->text_sel = i;  //记录需要改正的文字数组下表
                break;
            }
            d_all_store->p_text[i].path.translate(-this->width()/2,-this->height()/2);
            d_all_store->text_sel = -1;  //如果没有选中，则选回-1
            flag = 0;
       }
    }
    else flag = 0;
    painter.restore();

    //this->updacanvasPixmap(d_all_store);
    update();
}
